﻿/***
    This file is part of Qasparov.

    Qasparov is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Qasparov is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Qasparov. If not, see <http://www.gnu.org/licenses/>.

 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using Com.Oxilly.Qsb.Core.QBusConnector.Events;

namespace Com.Oxilly.Qsb.Core.QBusConnector
{
    class QBusManager
    {
        internal event EventHandler<ConnectionChangedEventArg> ConnectionChanged;
        internal event EventHandler<ModuleValueChangedEventArg> ModuleValueChanged;

        private QBusConnectionManager connectionManager;

        private List<Qbus.Communication.Protocol.Module> modules;        

        public List<Qbus.Communication.Protocol.Module> Modules
        {
            get
            {
                if (this.connectionManager == null)
                {
                     //TODO: Log Warning
                }

                return this.modules;
            }
        }

        
        public void Disconnect()
        {
            if (this.connectionManager != null)
            {
                this.connectionManager.Disconnect();
                this.connectionManager = null;
            }
            else
            {
                //TODO: Log Warning
            }
        }

        public void Connect()
        {
            if (this.connectionManager != null)
            {
                var conf = new QBusConfiguration
                {
                    //TODO: Retrieve settings. Not sure where to store them (AppConfig is so readable)
                    Adress = ConfigurationManager.AppSettings["QBUSHOST"],
                    Username = ConfigurationManager.AppSettings["QBUSUSER"],
                    Password = ConfigurationManager.AppSettings["QBUSPASS"],
                };
                this.connectionManager = new QBusConnectionManager(conf);
                this.connectionManager.ModuleLoaded += new EventHandler<Events.ModuleLoadedEventArg>(connectionManager_ModuleLoaded);
                this.connectionManager.ConnectionChanged += new EventHandler<Events.ConnectionChangedEventArg>(connectionManager_ConnectionChanged);
                this.connectionManager.Connect();
            }
            else 
            {
                //TODO: Log Warning
            }
        }

        void connectionManager_ConnectionChanged(object sender, Events.ConnectionChangedEventArg e)
        {
            if (this.ConnectionChanged != null)
            {
                this.ConnectionChanged(sender, e);
            }
        }

        void connectionManager_ModuleLoaded(object sender, Events.ModuleLoadedEventArg e)
        {
            //TODO: Log
            addModule(e.Module);
        }

        private void addModule(Qbus.Communication.Protocol.Module m)
        {
            if (this.modules == null)
            {
                this.modules = new List<Qbus.Communication.Protocol.Module>();
            }
            m.ValueChanged += new EventHandler(onModuleValueChanged);
            this.modules.Add(m);
          
        }

        void onModuleValueChanged(object sender, EventArgs e)
        {
            if (ModuleValueChanged != null)
            {
                ModuleValueChanged(this, new ModuleValueChangedEventArg(
                    module: sender as Qbus.Communication.Protocol.Module,
                    e: e
                    ));
            }
        }   
    }
}
